﻿using AjaxControlToolkit;
using Billing;
using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Billing.Controls.Reports
{
    public class ucGenerateReports : UserControl
    {
        private ClsAgency clsAgency = new ClsAgency();
        private ClsCostCentresInBU clsCostCentre = new ClsCostCentresInBU();
        private ClsReportTemplates clsRptTemp = new ClsReportTemplates();
        private ClsReports clsRpt = new ClsReports();
        //private DataTable dt;
        protected UpdatePanel UpdatePanel1;
        protected Panel PanelOwner;
        protected Label LblReportOwner;
        protected DropDownList ddlReportOwner;
        protected Panel PanelReportType;
        protected Label Label7;
        protected DropDownList ddlRptType;
        protected Panel PanelReports;
        protected UpdatePanel UpdatePanel3;
        protected Label Label4;
        protected DropDownList ddlReportType;
        protected UpdatePanel UpdatePanel2;
        protected Panel PanelAgency;
        protected Label Label2;
        protected DropDownList ddlAgency;
        protected Label Label3;
        protected DropDownList ddlBU;
        protected Label lblCostCentre;
        protected DropDownList ddlCostCentre;
        protected Panel PanelReportPeriod;
        protected Label Label5;
        protected TextBox txtStartDate;
        protected CalendarExtender CalendarExtender1;
        protected Label Label6;
        protected TextBox txtEndDate;
        protected CalendarExtender CalendarExtender2;
        protected Panel PanelBillingMonth;
        protected Label lblBillingMonth;
        protected DropDownList ddlMonth;
        protected DropDownList ddlYear;

        public DataTable ReportsGenerated
        {
            get
            {
                return (DataTable)this.ViewState["ReportsDownload"];
            }
            set
            {
                this.ViewState["ReportsDownload"] = value;
            }
        }

        public DataTable ReportsReleased
        {
            get
            {
                return (DataTable)this.ViewState["ReportsReleased"];
            }
            set
            {
                this.ViewState["ReportsReleased"] = value;
            }
        }

        public string ReportOwner
        {
            get
            {
                if (this.ddlReportOwner.SelectedIndex == -1)
                    return "0";
                else
                    return this.ddlReportOwner.SelectedItem.Text.ToLower();
            }
            set
            {
                this.ddlReportOwner.SelectedIndex = -1;
                this.BindReportOwner();
                this.ddlReportOwner.ClearSelection();
                try
                {
                    if (!(value != "") || !(value != "0"))
                        return;
                    this.ddlReportOwner.Items.FindByValue(value).Selected = true;
                }
                catch(Exception ex)
                {
                    log4net.Logger.Error("Exception:\r\n Type:" + ex.GetType() + ";\r\n Message:" + ex.Message + ";\r\n Trace:" + ex.StackTrace);
                }
            }
        }

        public string AgencyID
        {
            get
            {
                if (this.ddlAgency.SelectedIndex == -1 || this.ddlAgency.SelectedIndex == 0)
                    return "0";
                else
                    return this.ddlAgency.SelectedValue;
            }
            set
            {
                this.BindAgency();
                this.ddlAgency.SelectedIndex = -1;
                try
                {
                    this.ddlAgency.Items.FindByValue(value).Selected = true;
                }
                catch (Exception ex)
                {
                    log4net.Logger.Error("Exception:\r\n Type:" + ex.GetType() + ";\r\n Message:" + ex.Message + ";\r\n Trace:" + ex.StackTrace);
                }
                this.BindBU();
            }
        }

        public string CostCentreID
        {
            get
            {
                if (this.ddlCostCentre.SelectedIndex == -1 || this.ddlCostCentre.SelectedIndex == 0)
                    return "0";
                else
                    return this.ddlCostCentre.SelectedValue;
            }
            set
            {
                this.ddlCostCentre.SelectedIndex = -1;
                if (!(value != "0"))
                    return;
                this.ddlCostCentre.SelectedIndex = this.ddlCostCentre.Items.IndexOf(this.ddlCostCentre.Items.FindByValue(value));
            }
        }

        public string BUID
        {
            get
            {
                if (this.ddlBU.SelectedIndex == -1 || this.ddlBU.SelectedIndex == 0)
                    return "0";
                else
                    return this.ddlBU.SelectedValue;
            }
            set
            {
                this.BindBU();
                this.ddlBU.SelectedIndex = -1;
                try
                {
                    this.ddlBU.Items.FindByValue(value).Selected = true;
                }
                catch (Exception ex)
                {
                    log4net.Logger.Error("Exception:\r\n Type:" + ex.GetType() + ";\r\n Message:" + ex.Message + ";\r\n Trace:" + ex.StackTrace);
                }
                this.BindCostCentre();
            }
        }

        public string Reports
        {
            get
            {
                return this.ddlReportType.SelectedIndex.ToString();
            }
            set
            {
                this.ddlReportType.Items.FindByValue(value).Selected = true;
            }
        }

        public string ReportType
        {
            get
            {
                if (this.ddlRptType.SelectedIndex == -1)
                    return "1";
                else
                    return this.ddlRptType.SelectedValue;
            }
            set
            {
                this.ddlRptType.Items.FindByValue(value).Selected = true;
            }
        }

        public string ReportStartDate
        {
            get
            {
                return this.txtStartDate.Text;
            }
            set
            {
                this.txtStartDate.Text = value;
            }
        }

        public string ReportEndDate
        {
            get
            {
                return this.txtEndDate.Text;
            }
            set
            {
                this.txtEndDate.Text = value;
            }
        }

        public int ReportMonth
        {
            get
            {
                return this.ddlMonth.SelectedIndex + 1;
            }
            set
            {
                this.ddlMonth.Items.FindByValue(value.ToString()).Selected = true;
            }
        }

        public int ReportYear
        {
            get
            {
                if (this.ddlYear.SelectedIndex == -1)
                    return 0;
                else
                    return Convert.ToInt32(this.ddlYear.SelectedItem.Text);
            }
            set
            {
                this.ddlYear.Items.FindByValue(value.ToString()).Selected = true;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.Page.IsPostBack)
                return;
            this.BindData();
        }

        protected void setFormControls()
        {
            if (this.Page.ToString().ToLower().Contains("generate"))
                this.showReportPeriod(false);
            else if (this.Page.ToString().ToLower().Contains("download") || this.Page.ToString().ToLower().Contains("release"))
                this.showReportPeriod(true);
            if (this.ddlRptType.SelectedItem.Text.ToLower() != "summary")
                return;
            this.ddlCostCentre.Enabled = false;
        }

        protected void showAgency()
        {
            if (this.Page.ToString().ToLower().Contains("download") && LoginUser.GroupName.ToLower() == "key manager")
            {
                this.PanelAgency.Visible = false;
            }
            else
            {
                if (!this.Page.ToString().ToLower().Contains("generate"))
                    return;
                if (this.ddlReportOwner.SelectedItem.ToString().ToLower() == "key manager")
                    this.PanelAgency.Visible = true;
                else
                    this.PanelAgency.Visible = false;
            }
        }

        protected void showReportPeriod(bool flag)
        {
            this.PanelBillingMonth.Visible = flag;
            this.PanelReports.Visible = !flag;
            this.PanelOwner.Visible = !flag;
            this.PanelReportPeriod.Visible = !flag;
            this.showAgency();
        }

        protected void Page_LoadComplete(object sender, EventArgs e)
        {
        }

        protected void BindData()
        {
            this.BindReportOwner();
            this.BindReportType();
            this.BindAgency();
            this.BindReports();
            this.ddlYear.Items.Add((DateTime.Now.Year - 2).ToString());
            this.ddlYear.Items.Add((DateTime.Now.Year - 1).ToString());
            this.ddlYear.Items.Add(DateTime.Now.Year.ToString());
            this.ddlYear.SelectedIndex = 2;
            this.ddlMonth.SelectedIndex = DateTime.Now.Month;
            this.ddlAgency.SelectedIndex = 0;
            this.ddlBU.SelectedIndex = 0;
            this.ddlCostCentre.SelectedIndex = 0;
            this.setFormControls();
        }

        protected void BindReportType()
        {
            ClsReportType clsReportType = new ClsReportType();
            if (clsReportType.SelectAll() == null)
                return;
            this.ddlRptType = ClsWebControlHelper.GenerateControl("ReportTypeName", "ID", this.ddlRptType, clsReportType.SelectAll());
        }

        protected void BindReports()
        {
            this.clsRptTemp.ID = 0;
            this.clsRptTemp.ReportTypeID = Convert.ToInt64(this.ddlRptType.SelectedValue);
            this.clsRptTemp.UserTypeID = Convert.ToInt64(this.ddlReportOwner.SelectedValue);
            this.ddlReportType = ClsWebControlHelper.GenerateControl("ReportTitle", "ID", this.ddlReportType, this.clsRptTemp.SelectReportDetailsAll());
            this.ddlReportType.Items.Insert(0, "All");
            this.FilterReportData();
        }

        public void FilterReportData()
        {
            if (this.Page.ToString().ToLower().Contains("download"))
            {
                if (LoginUser.GroupName.ToLower() == "key manager")
                    this.ViewState["ReportsDownload"] = this.clsRpt.SelectReportsDownload(Convert.ToInt64(this.ReportType), LoginUser.BUID, 0, this.ReportMonth, this.ReportYear);
                else
                    this.ViewState["ReportsDownload"] = this.clsRpt.SelectReportsDownload(Convert.ToInt64(this.ReportType), Convert.ToInt64(this.BUID), Convert.ToInt64(this.CostCentreID), this.ReportMonth, this.ReportYear);
            }
            else
            {
                if (!this.Page.ToString().ToLower().Contains("release"))
                    return;
                DataView dataView = new DataView(this.clsRpt.SelectReportsReleased());
                if (this.BUID != "0")
                    dataView.RowFilter = string.Concat(new object[4]
                      {
                         "ReportTypeName like '%",
                         this.ddlRptType.SelectedItem.Text,
                         "%' and ReportForBU =",
                         Convert.ToInt64(this.BUID)
                      });
                else
                    dataView.RowFilter = "ReportTypeName like '%" + this.ddlRptType.SelectedItem.Text + "%'";
                this.ViewState["ReportsReleased"] = dataView.ToTable();
            }
        }

        protected void BindAgency()
        {
            this.ddlAgency = ClsWebControlHelper.GenerateControl("AgencyIDName", "ID", this.ddlAgency, this.clsAgency.SelectAll());
            this.ddlAgency.Items.Insert(0, "ALL");
            this.BindBU();
        }

        protected void BindReportOwner()
        {
            this.ddlReportOwner = ClsWebControlHelper.GenerateControl("GroupName", "GroupID", this.ddlReportOwner, new ClsGroupMember().SelectAll());
        }

        public void BindBU()
        {
            if (this.ddlAgency.SelectedIndex != -1 && this.ddlAgency.SelectedItem.Text.ToLower() != "all")
            {
                this.ddlBU = ClsWebControlHelper.GenerateControl("BUIDName", "ID", this.ddlBU, new ClsBUsInAgency()
                {
                    AgencyID = Convert.ToInt64(this.ddlAgency.SelectedValue)
                }.SelectByAgencyID());
                this.ddlBU.Items.Insert(0, "ALL");
            }
            this.BindCostCentre();
        }

        private void BindCostCentre()
        {
            if (this.ddlBU.SelectedIndex == -1)
                return;
            ClsCostCentresInBU costCentresInBU = new ClsCostCentresInBU();
            if (this.ddlBU.SelectedItem.Text.ToLower() != "all")
            {
                costCentresInBU.BUID = Convert.ToInt64(this.ddlBU.SelectedValue);
                this.ddlCostCentre = ClsWebControlHelper.GenerateControl("CostCentreIDName", "ID", this.ddlCostCentre, costCentresInBU.SelectByBUID());
                this.ddlCostCentre.Items.Insert(0, "ALL");
            }
        }

        protected void BindBillingYear()
        {
            this.ddlYear.Items.Add((DateTime.Now.Year - 2).ToString());
            this.ddlYear.Items.Add((DateTime.Now.Year - 1).ToString());
            this.ddlYear.Items.Add(DateTime.Now.Year.ToString());
        }

        protected void ddlAgency_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.ddlAgency.SelectedIndex == 0)
            {
                this.ddlBU.SelectedIndex = 0;
                this.ddlCostCentre.SelectedIndex = 0;
            }
            else
            {
                this.BindBU();
                this.BindReports();
            }
        }

        protected void ddlBU_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.BindCostCentre();
            this.BindReports();
        }

        protected void ddlReportOwner_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.showAgency();
            this.BindReports();
        }

        protected void ddlRptType_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.BindReports();
        }

        protected void ddlMonth_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.BindReports();
        }

        protected void ddlYear_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.BindReports();
        }
    }
}
